package cn.yangyumanage;

import android.os.Bundle;
import android.view.View;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.List;

import cn.yangyumanage.adapter.MainVpAdapter;
import cn.yangyumanage.fragment.BannerFragment;
import cn.yangyumanage.fragment.DouBanSearchFragment;
import cn.yangyumanage.fragment.FilmAddFragment;
import cn.yangyumanage.fragment.FilmManageFragment;
import cn.yangyumanage.fragment.LeaveMessageFragment;
import cn.yangyumanage.fragment.TagAllocationFragment;
import cn.yangyumanage.fragment.TagManageFragment;

public class MainActivity extends AppCompatActivity {

    private TabLayout tl_tab;
    private ViewPager2 vp2_manage;
    private AVLoadingIndicatorView aiv_douban_search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initPage();
    }

    //设置状态栏
    private void statusBarSetting() {
        Window window = getWindow();
        //设置状态栏颜色为透明
        window.setStatusBarColor(getResources().getColor(R.color.white, null));
        //设置状态栏字体颜色自行改变
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    public void initView() {
        statusBarSetting();
        tl_tab = findViewById(R.id.tl_tab);
        vp2_manage = findViewById(R.id.vp2_manage);
        aiv_douban_search = findViewById(R.id.aiv_douban_search);
    }

    //初始化页面
    private void initPage() {
        List<Fragment> fragments = new ArrayList<>();
        List<String> labelList = new ArrayList<>();
        FilmAddFragment filmAddFragment = new FilmAddFragment(this, aiv_douban_search);
        FilmManageFragment filmManageFragment = new FilmManageFragment(this);
        DouBanSearchFragment douBanSearchFragment = new DouBanSearchFragment(this, vp2_manage, filmAddFragment, aiv_douban_search);
        BannerFragment bannerFragment = new BannerFragment(this);
        LeaveMessageFragment leaveMessageFragment = new LeaveMessageFragment();
        TagManageFragment tagManageFragment = new TagManageFragment(this);
        TagAllocationFragment imageQualityManageFragment = new TagAllocationFragment(this);
        fragments.add(douBanSearchFragment);
        fragments.add(filmAddFragment);
        fragments.add(filmManageFragment);
        fragments.add(bannerFragment);
        fragments.add(leaveMessageFragment);
        fragments.add(tagManageFragment);
        fragments.add(imageQualityManageFragment);
        labelList.add("豆瓣搜索");
        labelList.add("影片添加");
        labelList.add("影库管理");
        labelList.add("横幅管理");
        labelList.add("留言管理");
        labelList.add("标签管理");
        labelList.add("标签分配");
        MainVpAdapter adapter = new MainVpAdapter(getSupportFragmentManager(), getLifecycle(), fragments);
        vp2_manage.setAdapter(adapter);
        vp2_manage.setCurrentItem(0);
        vp2_manage.setOffscreenPageLimit(20);

        new TabLayoutMediator(tl_tab, vp2_manage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(labelList.get(position));
            }
        }).attach();
    }
}